$(document).ready(function() {
	initSearchData();
});
var SEARCH_GOODS = 1; //商品名称
var SEARCH_TENANT_NAME = 2; //租户名称
var SEARCH_STOREHOUSE_ID = 4; //库房名称(ID)
var SEARCH_STOREHOUSE_NAME = 8; //库房名称(NAME)
var SEARCH_CREATE_TIME_START = 16; //建单开始时间
var SEARCH_CREATE_TIME_END = 32; //建单开始时间
var SEARCH_WAREHOUSE_IN_TIME_START = 64; //入库开始时间
var SEARCH_WAREHOUSE_IN_TIME_END = 128; //入库开始时间
var SEARCH_WAREHOUSE_OUT_TIME_START = 256; //出库开始时间
var SEARCH_WAREHOUSE_OUT_TIME_END = 512; //出库结束时间
var SEARCH_WAREHOUSE_FEE_TIME_START = 1024; //收费开始时间
var SEARCH_WAREHOUSE_FEE_TIME_END = 2048; //收费结束时间
var SEARCH_WAREHOUSE_IN_START = 4096;
var SEARCH_WAREHOUSE_SUM_START = 8192;
var SEARCH_WAREHOUSE_SUM_END = 16384;
var searchDataList = [{
		"name": "商品名称",
		"type": "GOODS_NAME",
		"className": "easyui-textbox",
		"id": "input_GOODS_NAME"
	}, {
		"name": "租户名称",
		"type": "TENANT_NAME",
		"className": "easyui-combobox",
		"id": "input_TENANT_NAME"
	}, {
		"name": "库房名称",
		"type": "STOREHOUSE_ID",
		"className": "easyui-combobox",
		"id": "input_STOREHOUSE_ID"
	}, {
		"name": "库房名称",
		"type": "STOREHOUSE_NAME",
		"className": "easyui-combobox",
		"id": "input_STOREHOUSE_NAME"
	}, {
		"name": "建单开始时间",
		"type": "CREATE_TIME_START",
		"className": "easyui-datebox",
		"id": "input_CREATE_TIME_START"
	}, {
		"name": "建单结束时间",
		"type": "CREATE_TIME_END",
		"className": "easyui-datebox",
		"id": "input_CREATE_TIME_END"
	},
	{
		"name": "入库开始时间",
		"type": "IN_DATE_START",
		"className": "easyui-datebox",
		"id": "input_IN_DATE_START"
	}, {
		"name": "入库结束时间",
		"type": "IN_DATE_END",
		"className": "easyui-datebox",
		"id": "input_IN_DATE_END"
	},
	{
		"name": "出库开始时间",
		"type": "OUT_DATE_START",
		"className": "easyui-datebox",
		"id": "input_OUT_DATE_START"
	}, {
		"name": "出库结束时间",
		"type": "OUT_DATE_END",
		"className": "easyui-datebox",
		"id": "input_OUT_DATE_END"
	},
	{
		"name": "收费开始时间",
		"type": "COST_DATE_START",
		"className": "easyui-datebox",
		"id": "input_COST_DATE_START"
	},
	{
		"name": "收费结束时间",
		"type": "COST_DATE_END",
		"className": "easyui-datebox",
		"id": "input_COST_DATE_END"
	}, {
		"name": "入库开始时间",
		"type": "IN_START",
		"className": "easyui-datebox",
		"id": "input_IN_START"
	},
	{
		"name": "统计开始时间",
		"type": "SUM_START",
		"className": "easyui-datebox",
		"id": "input_SUM_START"
	},
	{
		"name": "统计结束时间",
		"type": "SUM_END",
		"className": "easyui-datebox",
		"id": "input_SUM_END"
	},

];

//取得查询条件的对象数组
function getSearchOptionList(searchType, newdata) {
	var content = [];
	var s;
	var data = searchDataList;
	for(var i = 0; i < data.length; i++) {
		s = searchType & (1 << i);
		if(s != 0) {
			content.push(data[i]);
		}
	}
	if(newdata.length > 0) {
		for(var i = 0; i < newdata.length; i++) {
			content.push(newdata[i]);
		}
	}
	return content;
}

function constructSearchOptionFormat(type, newdata) {
	var data = getSearchOptionList(type, newdata);
	var label = 0;
	var content = '';
	for(var i = 0, len = data.length; i < len; i++) {
		if(label === 0) {
			content += '<tr><td width="10%">' + data[i].name + '</td><td width="23%"><input class="' +
				data[i].className + '" style="width: 100%" type="text" name="' + data[i].type +
				'" data-options="disabled:true" /></td>';
		} else if(label === 2) {
			content += '<td width="10%">' + data[i].name + '</td><td width="23%"><input class="' +
				data[i].className + '" style="width: 100%" type="text" name="' + data[i].type +
				'" data-options="disabled:true" /></td></tr>';
			label = -1;
		} else {
			content += '<td width="10%">' + data[i].name + '</td><td width="23%"><input class="' +
				data[i].className + '" style="width: 100%" type="text" name="' + data[i].type +
				'" data-options="disabled:true" /></td>';
		}
		if(i === len - 1 && label !== 2) {
			content += '</tr>';
		}
		label++;
	}
	content = '<form id="ff" action="#" method="post"><table cellpadding="2"  class="displaytable" style="text-align: left;width: 100%;margin: 0 auto;">' +
		content + '</table></form>';
	return content;
}

function buildSearchOptions(searchType, newdata) {

	var content = "";
	var s;
	var data = searchDataList;
	for(var i = 0; i < data.length; i++) {
		s = searchType & (1 << i);
		if(s != 0) {
			content += "<tr><td style=\"width:25%\;text-align:left;\">" + data[i].name + "</td><td style=\"width:75%\"><input data-options=\"required:" + data[i].required + "\" style=\"width: 80%\" class=\"" + data[i].className + "\" name=\"" + data[i].type + "\" id=\"" + data[i].id + "\" /></td></tr>"
		}
	}
	if(newdata.length > 0) {
		for(var i = 0; i < newdata.length; i++) {
			content += "<tr><td style=\"width:25%\;text-align:left;\">" + newdata[i].name + "</td><td style=\"width:75%\"><input style=\"width: 80%\" class=\"" + newdata[i].className + "\" name=\"" + newdata[i].type + "\" id=\"" + newdata[i].id + "\" /></td></tr>"
		}
	}
	var contain = "<table class=\"displaytable\">" + content + "</table>";
	contain += "<div style=\"text-align: center; margin-top:20px;\"><a href=\"#\" class=\"easyui-linkbutton\" onclick=\"doSearch()\" style=\"width:80px;\">查询</a>"
	contain += "<a href=\"#\" class=\"easyui-linkbutton\" onclick=\"closeForm()\" style=\"width:80px;margin-left:15px;\">取消</a></div>"
	return contain;
}

function processSearchCondition(isConstruct) {
	if($("#fm") == undefined) {
		console.log("请检查ID为div_search是否存在");
		return "";
	}
	var searchCondition = "";
	var data = {};
	$("#fm input[name]").each(function(index) {
		if($(this).val().trim().length != 0) {
			data[$(this).attr("name")] = $(this).val().trim();
			if(searchCondition == "") {
				searchCondition = $(this).attr("name") + "=" + $(this).val().trim();
			} else {
				searchCondition += "&" + $(this).attr("name") + "=" + $(this).val().trim();
			}
		}
	});
	if(isConstruct) {
		$('#ff').form('clear');
		$('#ff').form('load', data);
	}
	return searchCondition;
}

function initSearchData() {
	if($('#input_STOREHOUSE_ID') != undefined) {
		$('#input_STOREHOUSE_ID').combobox({
			valueField: 'ID',
			textField: 'NAME',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			url: RemoteLinkAddress + '/frozen/storehouse/listall.do',
		});
	}
	if($('#input_STOREHOUSE_NAME') != undefined) {
		$('#input_STOREHOUSE_NAME').combobox({
			valueField: 'NAME',
			textField: 'NAME',
			panelHeight: 200,
			mode: 'remote',
			method: 'get',
			url: RemoteLinkAddress + '/frozen/storehouse/listall.do',
		});
	}

	if($("#input_TENANT_NAME") != undefined) {
		$('#input_TENANT_NAME').combobox({
			valueField: 'TENANT_NAME',
			textField: 'TENANT_NAME',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			url: RemoteLinkAddress + '/frozen/tenant/listall.do',
			onChange: function(newValue, oldValue) {
				if(!isEnglish(newValue)) {
					return;
				}
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/frozen/tenant/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			}
		});
	}
	if($("#input_TENANT_NAME") != undefined) {
		$("#input_GOODS_NAME").combobox({
			valueField: 'NAME',
			textField: 'NAME',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			onChange: function(newValue, oldValue) {
				if(!isEnglish(newValue)) {
					return;
				}
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/frozen/goods/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			},
			onSelect: function(record) {
				//				$("#input_GOODS_ID").textbox('setValue', record.ID);
				//				$("#input_SPEC").textbox('setValue', record.SPEC); //规格
				//				$("#input_UNIT").textbox('setValue', record.UNIT); //单位
			}
		});
	}
}